package team18.yuejian.Activity;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import org.w3c.dom.Document;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import team18.yuejian.Model.MineModel;

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    public static  final int CHOOSE_PHOTO = 2;

    public Bitmap bitmap;
    public File PNG_Image;



    SharedPreferences sp;
    SharedPreferences.Editor editor;

    public void setUserId(int id){
        sp = getSharedPreferences("user",Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putInt("uid",id);
        editor.commit();
    }

    public int getUserId(){
        sp = getSharedPreferences("user",Context.MODE_PRIVATE);
        return sp.getInt("uid",0);
    }

    public void openAlbum(){
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, CHOOSE_PHOTO);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void handleImageOnKitkat(Intent data,ImageView imageView){
        String imagePath = null;
        Uri uri = data.getData();
        if (DocumentsContract.isDocumentUri(this, uri)){//如果是document的类型是Uri，择通过document id 处理
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())){
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" +id;

                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
                Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
            } else if ("content".equalsIgnoreCase(uri.getScheme())){
                imagePath = getImagePath(uri,null);
            }else if ("file".equalsIgnoreCase(uri.getScheme())){
                imagePath = uri.getPath();
            }

            displayImage(imagePath,imageView);
        }

    }

    public void handleImageBeforeKitKat(Intent data, ImageView imageView){
        Uri uri = data.getData();
        String imagePath = getImagePath(uri, null);
        displayImage(imagePath,imageView);
    }

    public String getImagePath(Uri uri, String selection){
        String path = null;
        Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
        if (cursor != null){
            if (cursor.moveToFirst()){
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

    public void displayImage(String imagePath, ImageView image){
        if (imagePath != null){
            bitmap = BitmapFactory.decodeFile(imagePath);
            image.setImageBitmap(bitmap);
        }
    }

    public void savePNG(Bitmap bitmap,String name){
        String SDPath = Environment.getExternalStorageDirectory().getPath() + "//";
        PNG_Image = new File(SDPath+name);
        try{
            FileOutputStream outputStream = new FileOutputStream(PNG_Image,false);
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)){
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getImageByView(ImageView imageView){
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap1 = Bitmap.createBitmap(imageView.getDrawingCache());
        imageView.setDrawingCacheEnabled(false);
        savePNG(bitmap1,"Mine_Logo.png");
        if (PNG_Image.exists()){
            Toast.makeText(this,"View找到图片",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"View未找到图片",Toast.LENGTH_SHORT).show();
        }

    }

    public static Date StrToDate(String str) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           Date date = null;
            try {
                 date = format.parse(str);
                } catch (ParseException e) {
                 e.printStackTrace();
                }
            return date;
         }

    @Override
    public void onClick(View view) {

    }



}
